<template>
  <div class="store-report-wrap" >
        <van-nav-bar
		  title="举报"
		  left-arrow
		  fixed
		  @click-left="onClickLeft"
		/>

        <div class="report-box" >
            <div class="report-title" >举报类型</div>

            <div class="report-ul" >
                <div 
                v-for="(item, index) in reportConfig"
                :key="item.id"
                @click="handleToSelect(index)"
                class="report-li active-btn" >
                    <div class="report-li-text" >{{ item.name }}</div>
                    <img class="report-li-icon" :src="item.select ? defaultTagIcon : defaultCircle" alt="">
                </div>
            </div>
            <div class="report-title no-require" >详细描述</div>
            <van-field
                v-model="content"
                rows="4"
                autosize
                type="textarea"
                maxlength="300"
                placeholder="填写投诉理由及相关证据"
                show-word-limit
            />

            <div class="report-title no-require" >上传凭证（最多3张）</div>

            <div class="upload-ul" >
                <van-uploader 
                v-model="fileList"
                :max-count="3" 
                :after-read="afterReadFront"
                :before-delete="uploadDleteFront"
                />
            </div>

        </div>
        
        <div @click="handleToSave" class="submit-btn active-btn" >提交</div>

  </div>
</template>

<script>
import { defaultTagIcon, defaultCircle } from '@/tool/static-img';
import { STORE_REPORT_CONFIG, STORE_REPORT_SUBMIT } from '@/api/store';
import {  OCR_RECOGNIZE } from "@/api/mine";
import { getOssParams, uploadImage, uploadDelete } from "@/api/upload";
export default {
    data() {
        return {
            defaultTagIcon, defaultCircle,
            reportConfig: [],
            content: '',
            fileList: [],

            ossParams: {},
            btnLoad: false
        }
    },
    created() {
        // 举报配置
        this.getReportConfig()
    },
    methods: {
        onClickLeft() {
            this.$router.back()
        },
        getReportConfig() {
            this.$http.get(STORE_REPORT_CONFIG)
                .then(res => {
                    res.data.forEach(item => {
                        item.select = false;
                    })
                    this.reportConfig = res.data;
                })
        },
        // 选择
        handleToSelect(index) {
            const reportConfigClone = JSON.parse(JSON.stringify(this.reportConfig));
                  reportConfigClone[index].select = !reportConfigClone[index].select;
                  this.reportConfig = reportConfigClone;
        },
        // 上传身份证正面
        async afterReadFront(file) {
            try {
                this.ossParams = await getOssParams(this.$http);
                // 调用封装的上传函数
                await uploadImage(file, this.ossParams, this.fileList);

                this.fileList = this.fileList.filter(item => {
                    return item.url
                })
                
            } catch (error) {
                console.error('处理上传错误:', error);
            }
        },
        // 删除
        uploadDleteFront(file) {
            this.fileList = uploadDelete(file, this.fileList);
        },
        handleToSave() {
            let params = {};
                params['store_id'] = this.$route.query.store_id;

                let config_ids = [];
                this.reportConfig.forEach(item => {
                    if(item.select) {
                        config_ids.push(item.id);
                    }
                })

                if(!config_ids.length) {
                    this.$toast('请选择举报类型')
                    return 
                }
                params['config_ids'] = config_ids;
                if(this.content.trim()) {
                    params['content'] = this.content;
                }

                // 图片
                let image_urls = [];
                console.log(this.fileList)
                this.fileList.forEach(item => {
                    image_urls.push(item.url)
                })

                params['image_urls'] = image_urls;

                if(this.btnLoad) {
                    return 
                }
                this.btnLoad = true;

                this.$http.post(STORE_REPORT_SUBMIT, params)
                    .then(res => {
                        this.onClickLeft();
                        this.$toast.success('举报成功');
                    })
                    .finally(() => {
                        this.btnLoad = false;
                    }) 

        }
    }
}

</script>
<style lang="scss" >
    .store-report-wrap{
        min-height: 100vh;
        padding-top: 46px;
        background-color: #f5f5f5;
        padding-bottom: calc(1.2rem + constant(safe-area-inset-bottom));
        padding-bottom: calc(1.2rem + env(safe-area-inset-bottom));
        box-sizing: border-box;
        .van-nav-bar .van-icon{
            color: #333333;
        }
        .van-nav-bar__arrow{
            font-size: 20px;
        }
        .report-box{
            padding: 0 0.32rem;
        }
        .report-title{
            height: 0.4rem;
            padding-top: 0.32rem;
            padding-bottom: 0.16rem;
            line-height: 0.4rem;
            font-size: 0.28rem;
            color: #333333;
            &::before{
                content: '*';
                margin-right: 0.04rem;
                color: #F9301B;
            }
            &.no-require{
                 &::before{
                     display: none;
                 }
            }
        }

        .report-ul{
            padding: 0.32rem;
            border-radius: 0.08rem;
            background: #ffffff;
            .report-li{
                display: flex;
                align-items: center;
                justify-content: space-between;
                margin-bottom: 0.28rem;
                &:last-child{
                    margin-bottom: 0;
                }
                .report-li-text{
                    font-size: 0.28rem;
                    font-weight: 400;
                    line-height: 0.4rem;
                    color: #333333;
                }
                .report-li-icon{
                    width: 0.32rem;
                    height: 0.32rem;
                }
            }
        }
        .van-field{
            border-radius: 0.08rem;
            .van-field__word-limit{
                color: #999999;
            }
        }
        .upload-ul{
            .upload-li{
                width: 2.12rem;
                height: 2.12rem;
                background: #ffffff;
            }

        }
        .van-uploader__upload{
            background: #ffffff;
        }
        .submit-btn{
            display: flex;
            align-items: center;
            justify-content: center;
            width: 6.06rem;
            height: 0.88rem;
            margin: auto;
            margin-top: 0.6rem;
            font-size: 0.32rem;
            color: #ffffff;
            border-radius: 0.44rem;
            background: linear-gradient( 270deg, #1261E2 0%, #4A89EC 100%);
        }
    }
</style>